#include <gtest/gtest.h>
#include <string>
#include "windows/security/cx_temp.h" 

using namespace CXLib;

TEST(CxTempTest, GetProcessTokenInfo_test) {
    
    ProcessTokenInfo token;
    DWORD ret = cxGetProcessTokenInfo(GetCurrentProcess(), token);

    EXPECT_EQ(ret, 0);
    EXPECT_FALSE(token.user.sid.empty());
    EXPECT_FALSE(token.user.name.empty());
    EXPECT_FALSE(token.user.domain.empty());

    auto jTokenStr = cxFormatProcessTokenInfo(token);
    EXPECT_FALSE(jTokenStr.empty());
}

TEST(CxTempTest, GetTokenPrivileges_test) {
    HANDLE hToken;
    if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) {
        FAIL() << "OpenProcessToken failed";
    }

    std::vector<PrivilegeInfo> privileges;
    DWORD ret = cxGetTokenPrivileges(hToken, privileges);
    CloseHandle(hToken);

    EXPECT_EQ(ret, 0);
    EXPECT_FALSE(privileges.empty());
}

TEST(CxTempTest, SetPrivilege_test) {
    HANDLE hToken = nullptr;

    ASSERT_TRUE(OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken));

    DWORD ret = cxSetPrivilege(hToken, TEXT("SeDebugPrivilege"), TRUE);
    EXPECT_EQ(ret, ERROR_SUCCESS);

    std::vector<PrivilegeInfo> privileges;
    ret = cxGetTokenPrivileges(hToken, privileges);
    EXPECT_EQ(ret, ERROR_SUCCESS);

    ret = cxSetPrivilege(hToken, TEXT("SeDebugPrivilege"), FALSE);
    EXPECT_EQ(ret, ERROR_SUCCESS);

    privileges.clear();
    ret = cxGetTokenPrivileges(hToken, privileges);
    EXPECT_EQ(ret, ERROR_SUCCESS);


    CloseHandle(hToken);
}
